<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode"  @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				supplierCache:"supplier-info-confirm-supplier-cache",
				orgNatureCache:"supplier-info-confirm-orgNature-cache",
				mtUserCache:"supplier-info-confirm-mtUser-cache",
				busCode:"",
				areaCache:"supplier-info-confirm-area-cache",
				provinceCache:"supplier-info-confirm-supplier-info-confirm-province-cache",
				countryId:0,
				provinceId:0,
				cityceCache:"supplier-info-confirm-supplier-info-confirm-city-cache",
				exists:['adjustTypeId','adjustTypeName','supplierCode','zposId','title0']
				
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId','orgId'])
		},
		methods:{
			verifyAdjustType(){
				let _that = this
				if(!_that.$refs.autoform.formdata.adjustTypeId){
					_that.om.toast('请先选择调整类别',3000)
					return false
				}else{
					return true
				}
			},
			verifyProvince(){
				let _that = this
				if(!_that.$refs.autoform.formdata.areaId){
					_that.om.toast('请先选择国家/地区',3000)
					return false
				}else{
					return true
				}
			},
			verifyCity(){
				let _that = this
				if(!_that.$refs.autoform.formdata.provinceId){
					_that.om.toast('请先选择省份',3000)
					return false
				}else{
					return true
				}
			},
			formSubmit(){
				let _that = this
				if(!_that.formdata.itemStr){
					_that.formdata.itemStr = "[]"
				}
				if(!_that.formdata.userStr){
					_that.formdata.userStr = "[]"
				}
				_that.formdata.orgId = this.orgId
				if(_that.edit){
					_that.om.formSave('tsupplierInfoConfirm/doUpdate',_that.formdata,_that)
				}else{
					_that.om.formSave('tsupplierInfoConfirm/doAdd',_that.formdata,_that)
				}
			},
			
			initProvince(id){
				let _that = this
				_that.$refs.autoform.changeEditor('title1', "provinceId", {
					link:"../../public/pageSearch/pageSearch",
						"linkParam": {
						other: JSON.stringify({
							parentid:id
						}),
						namefield:'dicname',
						searchfield:"q",
						initload:true,
						url:'regions/getCombobox',
						cachename:_that.provinceCache,
						placeholder:"请输入省份"
						}
				})
			},
			initCity(id){
				let _that = this
				_that.$refs.autoform.changeEditor('title1', "cityId", {
					link:"../../public/pageSearch/pageSearch",
					"linkParam": {
						other: JSON.stringify({
							parentid:id
						}),
						namefield:'dicname',
						searchfield:"q",
						initload:true,
						url:'regions/getCombobox',
						cachename:_that.cityceCache,
						placeholder:"请输入城市"
					}
				})
			},
			
			onSelectAdjustType(item){
				let _that = this
				if(!_that.edit){
					Object.keys(_that.$refs.autoform.formdata).forEach(key =>{
						if(!_that.exists.includes(key)){
							_that.$refs.autoform.formdata[key] = ''
						}})
				}
				if(item.id === 4845 ){
					_that.om.request(
						'tsupplierInfoConfirm/getSupplierCode',
						{},
						'post'
					).then(res=>{
						_that.$refs.autoform.formdata.supplierCode = res.value
					})
				}else if(item.id === 5344){
					_that.$refs.autoform.formdata.supplierCode = ''
					}
					_that.$refs.autoform.changeEditor('title1','supplierCode',{
						link:"../../public/pageSearch/pageSearch",
						"linkParam": {
							other: JSON.stringify({
							}),
							namefield:'supplierCode',
							notefield:'supplierName',
							searchfield:"q",
							initload:true,
							url:'tsupplierInfoConfirm/likeSerchSupplier',
							cachename:_that.supplierCache,
							placeholder:"请输入公司名称或代码"
						}
					})
					
					_that.om.request(
							"tsysdict/findChildrenByCodeNoIsdel1",
							{code:"38030"}
					).then(res =>{
						_that.$refs.autoform.changeEditor('title1','invoiceTypeId','data',configSelectData(res,'id','dicname'))
						
					})
					_that.om.request(
							"tsysdict/findChildrenByCodeNoIsdel1",
							{code:"36779"}
					).then(res =>{
						_that.$refs.autoform.changeEditor('title1','currencyTypeId','data',configSelectData(res,'id','dicname'))
					})
					_that.om.request(
							"tsysdict/findChildrenByCodeNoIsdel1",
							{code:"38033"}
					).then(res =>{
						_that.$refs.autoform.changeEditor('title1','supplierTypeId','data',configSelectData(res,'id','dicname'))
					})
					_that.om.request(
							"tsysdict/findChildrenByCodeNoIsdel1",
							{code:"23685"}
					).then(res =>{
						_that.$refs.autoform.changeEditor('title1','supplierCategoryId','data',configSelectData(res,'id','dicname'))
					})
					
				
			},
			onSelectZposId(item){
				let _that = this
				_that.om.request(
					"tsupplierInfoConfirm/findAdjustType",
					{
						code:_that.busCode
					}
				).then(res =>{
					_that.$refs.autoform.changeEditor('title1','adjustTypeId','data',configSelectData(res,'id','dicname'))
					if(_that.edit){
						_that.$refs.autoform.initSelectIndex('title1','adjustTypeId')
					}
				})
			},
			initLoad(options){
				let _that = this
				_that.busCode = options.code
				var ajaxArr = [
					urlUtil.zposUrl(options.code)
				];
				let fileId = options.fileId
				if(fileId){
					ajaxArr.push({
						url:"tsupplierInfoConfirm/getValue",
						param:{
							fileId:fileId
						}
						
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor('topinfo','zposId','data',configSelectData(res[0],'id','posname'),mainData)
					if(_that.subEditorObj["title2"]){
						changeEditor("title2", "userStr", {
							titleTemplate:"<%= '姓名:' + userName || '' %>",
							subtitle1Template:"<%= '供方角色:'+ roleName %>",
							subtitle2Template:"<%= '岗位职务:' + jobName %>",
							subtitle3Template:"<%= '手机号码:' + userMobile %>"
						}, null, mainData)
					}
					if(_that.subEditorObj["title3"]){
						changeEditor("title3", "itemStr", {
							titleTemplate:"<%= '附件名称:' + fileTypeName  %>"
						}, null, mainData)
					}
					changeEditor('title1','orgNatureId',{
						link:"../../public/pageSearch/pageSearch",
						"linkParam": {
							other: JSON.stringify({
							}),
							namefield:'dicname',
							searchfield:"q",
							initload:true,
							url:'organizationInstitutionCategory/getPageLeaf',
							cachename:_that.orgNatureCache,
							placeholder:"请输入公司性质"
						}
					},null,mainData)
					changeEditor('title4', "maintainUserId", {
						link:"../../public/pageSearch/pageSearch",
						"linkParam": {
							namefield:urlUtil.userPageUrl().namefield,
							searchfield:"q",
							initload:true,
							url:urlUtil.userPageUrl().url,
							cachename:_that.mtUserCache,
							placeholder:"请输入姓名或岗位"
						}
					},null,mainData)
					
					changeEditor('title1', "areaId", {
						link:"../../public/pageSearch/pageSearch",
						"linkParam": {
							namefield:'dicname',
							searchfield:"q",
							initload:true,
							url:'regions/getCountryList',
							cachename:_that.areaCache,
							placeholder:"请输入国家"
						}
					},null,mainData)
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push('id')
						for(let index in res[1].value){
							if(fieldArr.includes(index)){
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[1].value[index])
								_that.$set(_that.formdata,index,res[1].value[index])
							}
						}
						let userList = _that._.cloneDeep(res[1].value.userList)
						userList.forEach(e=>{
							if(!e.previousHistory){
								delete e.previousHistory
							}
						})
						_that.subEditorObj["title2"].workList = userList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "userStr", JSON.stringify(userList))
						_that.$set(_that.formdata, "userStr", JSON.stringify(userList))
						delete res[1].value.userList
						let itemList = _that._.cloneDeep(res[1].value.itemList)
						_that.subEditorObj["title3"].workList = itemList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "itemStr", JSON.stringify(itemList))
						itemList.forEach(e=>{
							if(!e.previousHistory){
								delete e.previousHistory
							}
						})
						_that.$set(_that.formdata, "itemStr", JSON.stringify(itemList))
						delete res[1].value.itemList
						_that.om.request(
							'orgInstPosUserOfficial/getPostList',
							{
								"user.id":_that.$refs.autoform.formdata.maintainUserId
							}
						).then(result=>{
							_that.$refs.autoform.changeEditor('title4','maintainPostId','data',configSelectData(result,'id','posname'))
							_that.$refs.autoform.initSelectIndex('title4','maintainPostId')
							})
						_that.$nextTick(function(){
							_that.onSelectZposId()
							_that.$refs.autoform.initSelectIndex('topinfo','zposId')
							_that.initProvince(_that.$refs.autoform.formdata.areaId)
							_that.initCity(_that.$refs.autoform.formdata.provinceId)
							_that.onSelectAdjustType({id:_that.$refs.autoform.formdata.adjustTypeId})
							_that.$refs.autoform.initSelectIndex('title1','orgNatureId')
							_that.$refs.autoform.initSelectIndex('title1','invoiceTypeId')
							_that.$refs.autoform.initSelectIndex('title1','currencyTypeId')
							_that.$refs.autoform.initSelectIndex('title1','supplierTypeId')
							_that.$refs.autoform.initSelectIndex('title1','supplierCategoryId')
							
						})
						
					}
				},options)
			}
		},
		onShow(){
			let _that = this;
			if(_that.$refs.autoform){
				for (var fieldId in _that.subEditorObj) {
					var editor = _that.subEditorObj[fieldId];
					uni.getStorage({
						key: getWorkCacheKey(fieldId),
						success: function(res) {
							editor.workList = res.data;
							uni.removeStorage({
								key: getWorkCacheKey(fieldId)
							});
						}
					});
				}
				uni.getStorage({
					key:_that.areaCache,
					success:function(res){
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.areaId = d.id
							formdata.areaName = d.dicname
							_that.countryId = d.id
							_that.initProvince(d.id)
							_that.$refs.autoform.clear('title1','provinceId')
							_that.$refs.autoform.clear('title1','cityId')
							uni.removeStorage({
								key:_that.areaCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.provinceCache,
					success:function(res){
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.provinceId = d.id
							formdata.provinceName = d.dicname
							_that.provinceId = d.id
							_that.initCity(d.id)
							_that.$refs.autoform.clear('title1','cityId')
							uni.removeStorage({
								key:_that.provinceCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.cityceCache,
					success:function(res){
						if(res.data){
							let formdata = _that.$refs.autoform.formdata
							let d = res.data
							formdata.cityId =d.id
							formdata.cityName = d.dicname
							uni.removeStorage({
								key:_that.cityceCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.mtUserCache,
					success:function(res){
						if(res.data){
							let d =res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.maintainUserId = d.id
							formdata.maintainUserName = d.username
							formdata.maintainInstId = d.salaryPosInstId
							formdata.maintainInstName = d.salaryPosInstName
							_that.om.request(
								'orgInstPosUserOfficial/getPostList',
								{
									"user.id":d.id
								}
							).then(res=>{
								if(res.length>1){
									_that.$refs.autoform.changeEditor('title4','maintainPostId','data',configSelectData(res,'id','posname'))
								}else{
									_that.$refs.autoform.changeEditor('title4','maintainPostId','data',configSelectData(res,'id','posname',true))
								}
								})
							uni.removeStorage({
								key:_that.mtUserCache
							})
						}
					}
				})
				
				uni.getStorage({
					key:_that.supplierCache,
					success:function(res){
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.supplierCode = d.supplierCode
							formdata.supplierName = d.supplierName
							formdata.tsupplierId = d.tsupplierId
							formdata.areaName = d.areaName
							formdata.areaId = d.areaId
							_that.countryId = d.areaId
							_that.initProvince(d.areaId)
							formdata.provinceName = d.provinceName
							formdata.provinceId = d.provinceId
							formdata.cityName = d.cityName
							formdata.cityId = d.cityId
							_that.initCity(d.cityId)
							formdata.supplierAddress = d.supplierAddress
							formdata.supplierShortName = d.supplierShortName
							formdata.orgSite = d.orgSite
							formdata.zipCode = d.zipCode
							formdata.supplierTelephone = d.supplierTelephone
							formdata.fax = d.fax
							formdata.unifiedSocialCreditCode = d.unifiedSocialCreditCode
							formdata.invoiceTypeName = d.invoiceTypeName
							formdata.invoiceTypeId = d.invoiceTypeId
							_that.$refs.autoform.initSelectIndex('title1','invoiceTypeId')
							formdata.orgNatureName = d.orgNatureName
							formdata.orgNatureId = d.orgNatureId
							formdata.taxRate = d.taxRate
							formdata.depositBank = d.depositBank
							formdata.openingAccount = d.openingAccount
							formdata.registeredCapital = d.registeredCapital
							formdata.currencyTypeId = d.currencyTypeId
							formdata.currencyTypeName = d.currencyTypeName
							_that.$refs.autoform.initSelectIndex('title1','currencyTypeId')
							formdata.supplierTypeId = d.supplierTypeId
							formdata.supplierTypeName = d.supplierTypeName
							_that.$refs.autoform.initSelectIndex('title1','supplierTypeId')
							formdata.supplierCategoryId = d.supplierCategoryId
							formdata.supplierCategoryName = d.supplierCategoryName
							_that.$refs.autoform.initSelectIndex('title1','supplierCategoryId')
							formdata.lastYearAnnualSales = d.lastYearAnnualSales
							formdata.orgCoverage = d.orgCoverage
							formdata.orgBuildingArea = d.orgBuildingArea
							formdata.totalEmployees = d.totalEmployees
							formdata.technician = d.technician
							formdata.qualitsyAssurancePersonnel = d.qualityAssurancePersonnel
							formdata.producer = d.producer
							formdata.productionEquipment = d.productionEquipment
							formdata.testEquipment = d.testEquipment
							formdata.meteringEquipment = d.meteringEquipment
							formdata.supplierProfile = d.supplierProfile
							formdata.businessScope = d.businessScope
							formdata.supplyCapability = d.supplyCapability
							formdata.qualityAssuranceCapability = d.qualityAssuranceCapability
							formdata.majorCustomers = d.majorCustomers
							formdata.provideProducts = d.provideProducts
							formdata.similarSupplierSum = d.similarSupplierSum
							formdata.outsidePurchaseFlag = d.outsidePurchaseFlag
							formdata.outsideAssistFlag = d.outsideAssistFlag
							formdata.serviceFlag = d.serviceFlag
							formdata.keyPartsFlag = d.keyPartsFlag
							let obj = {
								adjustTypeId:_that.$refs.autoform.formdata.adjustTypeId,
								supplierCode:_that.$refs.autoform.formdata.supplierCode,
								zuserId:_that.userId
							}
							_that.om.request(
							"tsupplierInfoConfirm/getSupplier",
							obj
							).then(result =>{
								if(res.succeed){
									_that.subEditorObj["title2"].workList = result.value.userList
									_that.subEditorObj["title3"].workList = result.value.itemList	
								}else{
									if(res.msg){
										_that.om.toast(res.msg,3000)
									}
								}
														
								})
							
							formdata.orgNatureName = d.orgNatureName
							formdata.orgNatureId = d.orgNatureName
							formdata.maintainUserId = d.maintainUserId
							formdata.maintainUserName = d.maintainUserName
							formdata.maintainInstId = d.maintainInstId
							formdata.maintainInstName = d.maintainInstName
							formdata.maintainPostId = d.maintainPostId
							formdata.maintainPostName = d.maintainPostName
							_that.om.request(
								'orgInstPosUserOfficial/getPostList',
								{
									"user.id":d.maintainUserId
								}
							).then(res=>{
								_that.$refs.autoform.changeEditor('title4','maintainPostId','data',configSelectData(res,'id','posname'))
								_that.$refs.autoform.initSelectIndex('title4','maintainPostId')
								})
							uni.removeStorage({
								key:_that.supplierCache
							})
							
						}
					}
				})
				uni.getStorage({
					key:_that.orgNatureCache,
					success:function(res){
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.orgNatureName = d.dicname
							formdata.orgNatureId = d.id
							d.salaryPosInstName
							uni.removeStorage({
								key:_that.orgNatureCache
							})
						}
					}
				})
				
			}
		},
		onLoad(options){
			this.initLoad(options)
		}
	}
</script>

<style>
</style>
